package com.atgouwu.mall.product.service.impl;

import com.atgouwu.mall.product.entity.BrandEntity;
import com.atgouwu.mall.product.entity.SkuInfoEntity;
import com.atgouwu.mall.product.service.BrandService;
import com.atgouwu.mall.product.service.SkuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atgouwu.mall.common.utils.PageUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.atgouwu.mall.product.dao.SpuInfoDao;
import com.atgouwu.mall.product.entity.SpuInfoEntity;
import com.atgouwu.mall.product.service.SpuInfoService;

@Service("spuInfoService")
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoDao, SpuInfoEntity> implements SpuInfoService {
    @Autowired
    private SkuInfoService skuInfoService;
    @Autowired
    private BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuInfoEntity> page = this.page(
        new Page<>(Long.valueOf(String.valueOf(params.get("page"))),Long.valueOf(String.valueOf(params.get("size")))),
                new QueryWrapper<SpuInfoEntity>()
        );
        return new PageUtils(page.getRecords(), (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
    }

    //通过skuId获取spu
    @Override
    public SpuInfoEntity getSpuInfoBySkuId(Long skuId) {
        SkuInfoEntity skuInfoEntity = skuInfoService.getById(skuId); //先根据skuId获取spuId
        Long spuId = skuInfoEntity.getSpuId();
        SpuInfoEntity spuInfoEntity = baseMapper.selectById(spuId); //再通过spuId获取spu数据和brandId
        BrandEntity brandEntity = brandService.getById(spuInfoEntity.getBrandId()); //根据brandId获取品牌
        spuInfoEntity.setBrandName(brandEntity.getName());
        return spuInfoEntity;
    }

}